function levelOrder(root) {
    let res = []
    if (!root) {
        return res
    }

    const q = []
    q.push(root)
    while (q.length !== 0) {
        const currentlevel = q.length
        res.push([])
        for (let i = 0; i < currentlevel; i++) {
            const node = q.shift()
            res[res.length - 1].push(node.val)
            if (node.left) q.push(node.left)
            if (node.right) q.push(node.right)
        }
    }
    return res
}